﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VatTuDTO;
using MySql.Data.MySqlClient;
using System.Data;

namespace VatTuDAO
{
    public class ChungLoaiDAO : BaseDataProvider
    {
        MySqlCommand _cmd; 
        public List<ChungLoaiDTO> SelectChungLoai()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChungLoai";
            _cmd.CommandType = CommandType.Text;
         

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            List<ChungLoaiDTO> ds = new List<ChungLoaiDTO>();
            ChungLoaiDTO dto;
            foreach (DataRow row in dt.Rows)
            {
                dto = new ChungLoaiDTO();
                dto.MaChungLoai = int.Parse(row["MaChungLoai"].ToString());
                dto.TenChungLoai= row["TenChungLoai"].ToString();
                ds.Add(dto);
            }
            return ds;
        }

        public ChungLoaiDTO SelectChungLoai(int maChungLoai)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChungLoai where MaChungLoai='"+maChungLoai+"'";
            _cmd.CommandType = CommandType.Text;


            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            
            ChungLoaiDTO dto;
            DataRow row =dt.Rows[0];         
            dto = new ChungLoaiDTO();
                dto.MaChungLoai = int.Parse(row["MaChungLoai"].ToString());
                dto.TenChungLoai = row["TenChungLoai"].ToString();
               
            
            return dto;
        }
    }
}
